<?php

namespace app\admin\controller\general;

use app\admin\model\member\Account;
use app\admin\model\Admin;
use app\admin\model\AdminBasicZoom;
use app\common\controller\Backend;
use Exception;
use fast\Random;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Session;
use think\Validate;

/**
 * 个人配置
 *
 * @icon fa fa-user
 */
class Profile extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
//        if ($this->request->isAjax()) {
//            $this->model = model('AdminLog');
//            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//
//            $list = $this->model
//                ->where($where)
//                ->where('admin_id', $this->auth->id)
//                ->order($sort, $order)
//                ->paginate($limit);
//
//            $result = array("total" => $list->total(), "rows" => $list->items());
//            return json($result);
//        }
        $this->model = model('Admin');
        $r = $this->model->get(['id'=>$this->auth->id]);
        $r = $r->toArray();
        $bmodel =  model('AdminBasicZoom');//个人基本信息
        $b = $bmodel->get(['admin_id'=>$this->auth->id]);
        $b = $b->toArray();
        $r['intro'] = $b['intro'];

        $cmodel = new \app\admin\model\member\Account;
        $r['catArr'] = $cmodel->getCatByUid($this->auth->id);
        $r['tagsArr'] = $cmodel->getTagsByUid($this->auth->id);
        $this->view->assign("r", $r);
        return $this->view->fetch();
    }

    /**
     * 更新个人信息
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $p['intro'] = $params['intro'];
            $p['email'] = $params['email'];
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('email', 'nickname', 'avatar', 'password'))
            ));
            unset($v);
            if (!Validate::is($params['email'], "email")) {
                $this->error(__("Please input correct email"));
            }


            if (isset($params['password'])) {
                if (!Validate::is($params['password'], "/^[\S]{6,16}$/")) {
                    $this->error(__("Please input correct password"));
                }
                $params['salt'] = Random::alnum();
                $params['password'] = md5(md5($params['password']) . $params['salt']);
            }
            //pr($params);
           // $exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
            $exist = AdminBasicZoom::where('email', $params['email'])->where('admin_id', '<>', $this->auth->id)->find();
            if ($exist) {
                $this->error(__("Email already exists"));
            }
            Db::startTrans();
            try {
                if ($params) {
                    //$admin = Admin::get($this->auth->id);
                    $admin = Admin::get(['id'=>$this->auth->id]);
                    unset($params['re_password']);
                    $admin->save($params);
                    $admin  = $admin->toArray();
                    $account = Account::get(['id'=>$admin['account_id']]);
                    $account->save($params);

                    $admin_basic = AdminBasicZoom::get(['admin_id'=>$this->auth->id]);
                    $admin_basic->save($p);

                    $r = $admin_basic->toArray();
                    $admin['intro'] = $r['intro'];
                    $admin['avatar'] = $r['avatar'];
//                    $r['id'] = $admin['id'];
//                    $r['username'] = $admin['username'];
//                    $r['nickname'] = $admin['nickname'];
//                    $r['mobile'] = $admin['mobile'];
//                    $r['createtime'] = $r['ctime'];
//                    $r['updatetime'] = $r['utime'];
                    //$r['token'] = $admin['token_zoom'];

//                    //因为个人资料面板读取的Session显示，修改自己资料后同时更新Session
                    Session::set("admin", $admin);
                    Db::commit();
                    $this->success();
                }
            } catch (ValidateException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

        }
        return;
    }
}
